package com.lmy.demo0.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.lmy.demo0.common.Result;
import java.util.List;

import com.lmy.demo0.service.IDeptService;
import com.lmy.demo0.entity.Dept;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingzi
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private IDeptService deptService;

            // 新增或者更新
            @PostMapping
            public Result save(@RequestBody Dept dept) {
                    return Result.success(deptService.saveOrUpdate(dept));
        }

            @DeleteMapping("/{id}")
            public Result delete(@PathVariable Integer id) {
                    return Result.success(deptService.removeById(id));
                    }

            @PostMapping("/del/batch")
            public Result deleteBatch(@RequestBody List<Integer> ids) {
                    return Result.success(deptService.removeByIds(ids));
                    }

            @GetMapping
            public Result findAll() {
                    return Result.success(deptService.list());
                    }

            @GetMapping("/{id}")
            public Result findOne(@PathVariable Integer id) {
                    return Result.success(deptService.getById(id));
                    }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam String name,
        @RequestParam Integer pageSize) {
                QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                queryWrapper.like("name",name);
                return Result.success(deptService.page(new Page<>(pageNum, pageSize), queryWrapper));

}
        }

